<?php

namespace App\Admin\Controllers;

use App\Models\EmailAlert;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class EmailAlertController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(EmailAlert::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->email('Email');

            $grid->column('Options')->display(function() {

                $options = 'Table of Contents: <strong>' . ($this->table_of_content ? 'Yes' : 'No') . '</strong><br>';
                $options .= 'As soon as published: <strong>' . ($this->asap_published ? 'Yes' : 'No') . '</strong><br>';
                $options .= 'Search Keywords: <strong>' . ($this->search_keywords ? 'Yes' : 'No') . '</strong>' . ($this->Keywords ? ', Keywords: ' . $this->Keywords : '') . '<br>';
                $options .= 'Highlight article published: <strong>' . ($this->high_light ? 'Yes' : 'No') . '</strong><br>';


                return $options;
            });
            $grid->created_at();
            $grid->updated_at();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(EmailAlert::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->email('email', 'Email');

            $states = [
                'Yes'  => ['value' => 1, 'text' => 'Yes', 'color' => 'success'],
                'No' => ['value' => 0, 'text' => 'No', 'color' => 'danger'],
            ];
            $form->switch('table_of_content', 'Table of Content')->states($states);
            $form->switch('asap_published', 'ASAP published')->states($states);
            $form->switch('search_keywords', 'Search Keywords')->states($states);
            $form->text('keywords', 'Keywords');
            $form->switch('high_light', 'Highlight article published')->states($states);


            
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->tools(function(\Encore\Admin\Form\Tools $tools) {
                $tools->disableView();
            });
        });
    }
}
